package com.castle.fortress.admin.study.course.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.study.course.entity.StudyCourseLogEntity;
import com.castle.fortress.admin.study.course.dto.StudyCourseLogDto;
import com.castle.fortress.admin.study.course.service.StudyCourseLogService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.castle.fortress.admin.utils.ExcelUtils;
import javax.servlet.http.HttpServletResponse;
import com.castle.fortress.common.entity.DynamicExcelEntity;
import java.util.List;

/**
 * 课程记录表 控制器
 *
 * @author castle
 * @since 2023-02-14
 */
@Api(tags="课程记录表管理控制器")
@Controller
public class StudyCourseLogController {
    @Autowired
    private StudyCourseLogService studyCourseLogService;

    /**
     * 课程记录表的分页展示
     * @param studyCourseLogDto 课程记录表实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="课程记录表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("课程记录表分页展示")
    @GetMapping("/study/course/studyCourseLog/page")
    @ResponseBody
    @RequiresPermissions("study:course:studyCourseLog:pageList")
    public RespBody<IPage<StudyCourseLogDto>> pageStudyCourseLog(StudyCourseLogDto studyCourseLogDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<StudyCourseLogDto> page = new Page(pageIndex, pageSize);
        IPage<StudyCourseLogDto> pages = studyCourseLogService.pageStudyCourseLog(page, studyCourseLogDto);

        return RespBody.data(pages);
    }

    /**
     * 课程记录表的列表展示
     * @param studyCourseLogDto 课程记录表实体类
     * @return
     */
    @CastleLog(operLocation="课程记录表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("课程记录表列表展示")
    @GetMapping("/study/course/studyCourseLog/list")
    @ResponseBody
    public RespBody<List<StudyCourseLogDto>> listStudyCourseLog(StudyCourseLogDto studyCourseLogDto){
        List<StudyCourseLogDto> list = studyCourseLogService.listStudyCourseLog(studyCourseLogDto);
        return RespBody.data(list);
    }

    /**
     * 课程记录表保存
     * @param studyCourseLogDto 课程记录表实体类
     * @return
     */
    @CastleLog(operLocation="课程记录表",operType = OperationTypeEnum.INSERT)
    @ApiOperation("课程记录表保存")
    @PostMapping("/study/course/studyCourseLog/save")
    @ResponseBody
    @RequiresPermissions("study:course:studyCourseLog:save")
    public RespBody<String> saveStudyCourseLog(@RequestBody StudyCourseLogDto studyCourseLogDto){
        if(studyCourseLogDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyCourseLogEntity studyCourseLogEntity = ConvertUtil.transformObj(studyCourseLogDto,StudyCourseLogEntity.class);
        if(studyCourseLogService.save(studyCourseLogEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 课程记录表编辑
     * @param studyCourseLogDto 课程记录表实体类
     * @return
     */
    @CastleLog(operLocation="课程记录表",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("课程记录表编辑")
    @PostMapping("/study/course/studyCourseLog/edit")
    @ResponseBody
    @RequiresPermissions("study:course:studyCourseLog:edit")
    public RespBody<String> updateStudyCourseLog(@RequestBody StudyCourseLogDto studyCourseLogDto){
        if(studyCourseLogDto == null || studyCourseLogDto.getId() == null || studyCourseLogDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyCourseLogEntity studyCourseLogEntity = ConvertUtil.transformObj(studyCourseLogDto,StudyCourseLogEntity.class);
        if(studyCourseLogService.updateById(studyCourseLogEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 课程记录表删除
     * @param id
     * @return
     */
    @CastleLog(operLocation="课程记录表",operType = OperationTypeEnum.DELETE)
    @ApiOperation("课程记录表删除")
    @PostMapping("/study/course/studyCourseLog/delete")
    @ResponseBody
    @RequiresPermissions("study:course:studyCourseLog:delete")
    public RespBody<String> deleteStudyCourseLog(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(studyCourseLogService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 课程记录表批量删除
     * @param ids
     * @return
     */
    @CastleLog(operLocation="课程记录表",operType = OperationTypeEnum.DELETE)
    @ApiOperation("课程记录表批量删除")
    @PostMapping("/study/course/studyCourseLog/deleteBatch")
    @ResponseBody
    @RequiresPermissions("study:course:studyCourseLog:deleteBatch")
    public RespBody<String> deleteStudyCourseLogBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(studyCourseLogService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 课程记录表详情
     * @param id 课程记录表id
     * @return
     */
    @CastleLog(operLocation="课程记录表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("课程记录表详情")
    @GetMapping("/study/course/studyCourseLog/info")
    @ResponseBody
    @RequiresPermissions("study:course:studyCourseLog:info")
    public RespBody<StudyCourseLogDto> infoStudyCourseLog(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyCourseLogEntity studyCourseLogEntity = studyCourseLogService.getById(id);
        StudyCourseLogDto studyCourseLogDto = ConvertUtil.transformObj(studyCourseLogEntity,StudyCourseLogDto.class);

        return RespBody.data(studyCourseLogDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation="课程记录表",operType = OperationTypeEnum.EXPORT)
	@PostMapping("/study/course/studyCourseLog/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<StudyCourseLogDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<StudyCourseLogDto> list = studyCourseLogService.listStudyCourseLog(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
